<template>
  <div>
    <!-- 单日五线白晚班 -->
    <Echart
      id="centerLeftChart"
      :options="options"
      height="400px"
      width="100%"
    />
  </div>
</template>

<script>
import Echart from '@/common/echart'
export default {
  components: {
    Echart
  },
  props: {
    cdata: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      options: {}
    }
  },
  watch: {
    cdata: {
      handler(newData) {
        this.options = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {
            left: 'center',
            top: 20,
            data: ['白班', '夜班']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '20%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: newData.dataList
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '白班',
              type: 'bar',
              stack: 'Ad',
              barWidth: 20,
              itemStyle: {
                borderRadius: 0,
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#FEC42C' },
                  { offset: 1, color: 'rgba(254,196,44,0.5)' }
                ])
              },
              emphasis: {
                focus: 'series'
              },
              label: {
                show: true,
                formatter: function(param) {
                  if (param.value === 0) {
                    return ''
                  } else {
                    return param.value
                  }
                },
                // position: 'right',
                offset: [25, -10],
                fontSize: 12
              },
              data: this.formatData(newData.dayList)
            },
            {
              name: '夜班',
              type: 'bar',
              stack: 'Ad',
              barWidth: 20,
              itemStyle: {
                borderRadius: 0,
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#4A81F1' },
                  { offset: 1, color: 'rgba(74,129,241,0.5)' }
                ])
              },
              emphasis: {
                focus: 'series'
              },
              label: {
                show: true,
                formatter: function(param) {
                  if (param.value === 0) {
                    return ''
                  } else {
                    return param.value
                  }
                },
                // position: 'right',
                offset: [25, -10],
                fontSize: 12
              },
              data: this.formatData(newData.nightList)
            }
          ]
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    formatData(data) {
      return data.map(item => Math.round(item))
    }
  }
}
</script>
